import multer from 'multer';
import path from 'path';
import fs from 'fs';

const uploadDirectory = 'uploads';

// 检查文件夹是否存在，如果不存在，则创建
if (!fs.existsSync(uploadDirectory)) {
    fs.mkdirSync(uploadDirectory, { recursive: true });
}

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadDirectory); // 确保此文件夹已存在
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + path.extname(file.originalname)); // 使用时间戳作为文件名
    }
});

const upload = multer({ storage: storage, limits: { fieldSize: 50 * 1024 * 1024 } });

export default upload;
